package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.pojo.Receives;
import com.luchao.back.service.ReceivesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "地址管理")
@RestController
@RequestMapping("/receives")
public class ReceivesController {

    @Autowired
    private ReceivesService receivesService;

    @ApiOperation("根据用户ID获取地址列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ruid", value = "用户ID", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getReceivesByUserId")
    public ResponseResult getReceivesByUserId(Integer ruid) {
        return ResponseResult.success(receivesService.getReceivesByUserId(ruid));
    }

    @ApiOperation("添加地址")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "receives", value = "地址对象信息", required = true, paramType = "form", dataType = "Receives", dataTypeClass = Receives.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addReceives")
    public ResponseResult addReceives(Receives receives) {
        return receivesService.addReceives(receives) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("修改地址信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "receives", value = "地址对象信息", required = true, paramType = "form", dataType = "Receives", dataTypeClass = Receives.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/editReceives")
    public ResponseResult editReceives(Receives receives) {
        return receivesService.editReceives(receives) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "修改成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "修改失败", Execute.ERROR);
    }

    @ApiOperation("修改地址信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "recid", value = "地址ID", required = true, paramType = "query", dataType = "int", dataTypeClass = int.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/delReceives")
    public ResponseResult delReceives(int recid) {
        return receivesService.delReceives(recid) ? ResponseResult.success(ResponseResult.SUCCESS, "删除成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "删除失败,有订单正在使用该地址", Execute.ERROR);
    }

    @ApiOperation("根据地址ID获取地址信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "recid", value = "地址ID", required = true, paramType = "query", dataType = "int", dataTypeClass = int.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getReceivesByRecid")
    public ResponseResult getReceivesByRecid(int recid) {
        return ResponseResult.success(receivesService.getReceivesByUserId(recid));
    }

    @ApiOperation("设置默认地址")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "recid", value = "地址ID", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = ""),
            @ApiImplicitParam(name = "ruid", value = "用户ID", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/setDefaultReceives")
    public ResponseResult setDefaultReceives(Integer recid, Integer ruid) {
        return receivesService.setDefaultReceives(recid, ruid) ? ResponseResult.success(ResponseResult.SUCCESS, "默认地址设置成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "默认地址设置失败", Execute.ERROR);
    }
}
